package com.bilibili.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bilibili.dubbo.api.db.UserInfoApi;
import com.bilibili.dubbo.api.mapper.UserInfoMapper;
import com.bilibili.model.db.UserInfo;
import com.bilibili.model.mongo.UserLike;
import com.bilibili.model.vo.PageResult;
import com.bilibili.model.vo.UserInfoVoLike;
import org.apache.commons.lang3.RandomUtils;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author HuangRongFu
 * 2021/11/17 星期三11:39 上午
 */

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 完善用户信息
     *
     * @params: userInfo
     * @Return:
     */
    @Override
    public void add(UserInfo userInfo) {

        userInfoMapper.insert(userInfo);
    }

    /**
     * 更新用户信息
     *
     * @params: userInfo
     */
    @Override
    public void update(UserInfo userInfo) {

        userInfoMapper.updateById(userInfo);
    }

    /**
     * 通过id查询用户基本信息
     *
     * @return
     * @params: userId
     */
    @Override
    public UserInfo findById(Long userId) {

        return userInfoMapper.selectById(userId);
    }

    /**
     * 条件批量id查询
     *
     * @param: userIds
     * @param: userInfoCondition
     * @Return: Map<Long, UserInfo>
     */
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo userInfoCondition) {

        QueryWrapper<UserInfo> qw = new QueryWrapper<>();

        if (null != userInfoCondition) {
            if (null != userInfoCondition.getGender()) {

                qw.eq("gender", userInfoCondition.getGender());
            }

            if (null != userInfoCondition.getAge()) {

                qw.le("age", userInfoCondition.getAge());
            }

            if (null != userInfoCondition.getNickname()) {

                qw.like("nickname", userInfoCondition.getNickname());
            }
        }
        qw.in("id", userIds);

        List<UserInfo> userInfos = userInfoMapper.selectList(qw);

        Map<Long, UserInfo> userInfoMap = new HashMap<>();

        if (CollectionUtils.isNotEmpty(userInfos)) {

            userInfoMap = userInfos.stream().collect(Collectors.toMap(UserInfo::getId, u -> u));
        }

        return userInfoMap;
    }

    /**
     * 通过ids批量查询用户信息
     *
     * @return
     * @param: userIds
     */
    @Override
    public List<UserInfo> findByBatchIds(List<Long> ids) {

        return userInfoMapper.selectBatchIds(ids);
    }

    /**
     * 用户管理 分页查询
     *
     * @param page
     * @param pageSize
     * @Return: PageResult
     */
    @Override
    public PageResult findPage(Long page, Long pageSize) {

        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(new Page<>(page, pageSize), null);

        return new PageResult(page, pageSize, userInfoPage.getTotal(), userInfoPage.getRecords());
    }

    /**
     * 互相喜欢的用户 喜欢用户 粉丝
     *
     * @param page
     * @param pageSize
     * @param ids
     * @return
     */
    @Override
    public PageResult<UserInfoVoLike> findFansPage(Long page, Long pageSize, List<Long> ids, Integer type) {

        //互相喜欢用户
        List<UserInfo> userInfoList = findByBatchIds(ids);

        QueryWrapper<UserInfo> qw = new QueryWrapper<>();

        qw.in("id", ids);

        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(new Page<>(page, pageSize), qw);

        long pages = userInfoPage.getPages();

        List<UserInfoVoLike> voList = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {

            UserInfoVoLike vo = new UserInfoVoLike();
            BeanUtils.copyProperties(userInfo, vo);

            voList.add(vo);
        }

        if(type == 1 || type == 2){

            voList.stream().forEach(u -> u.setAlreadyLove(true));
        }

        voList.stream().forEach(u -> u.setMatchRate(RandomUtils.nextInt(30, 100)));

        PageResult<UserInfoVoLike> pageResult = new PageResult<>(userInfoPage.getTotal(), pageSize, pages, page, voList);

        return pageResult;

    }


}
